package com.example.springbootdemo.controller;


import com.example.springbootdemo.po.Dept;
import com.example.springbootdemo.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Api(tags = "部门管理控制器")
public class DeptController {
    //注入service接口
  @Autowired
  private DeptService deptService;


 @RequestMapping("/index")
 @ApiOperation("系统首页")
 public String index(Model model){
     List<Dept> list = this.deptService.queryDept(new Dept());
     model.addAttribute("list",list);
//     session.setAttribute("admin","系统管理员");
     return "index";
 }
    @RequestMapping("/query")
    @ApiOperation("跳转页面")
    public String query(){
        return "list";
    }


    @RequestMapping("/gotoAdd")
    @ApiOperation("跳转添加页面")
       public String gotAdd(){
     return "add";

    }
//添加部门
    @PostMapping("/saveDept")
    @ApiOperation("保存部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dept",value = "部门对象",dataType = "Dept")
    })
    public String saveDept(Dept dept,Model model){
     this.deptService.addDept(dept);

        return this.index(model);
    }
//删除

    @GetMapping("/del")
    @ApiOperation("删除部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptNo",value = "部门编号",dataType = "String")
    })
    public String del(String deptNo,Model model){
     this.deptService.delDept(Integer.valueOf(deptNo));
        return this.index(model);
    }
//跳转到修改页面
    @RequestMapping("/gotoEdit/{deptNo}")
    public String gotoEdit(@PathVariable("deptNo") String deptNo, Model model){
        System.out.println("============"+deptNo);
        Dept dept = new Dept();
        dept.setDeptNo(Integer.valueOf(deptNo));
        //根据部门编号有且只有一个
        List<Dept> list = this.deptService.queryDept(dept);
          model.addAttribute("dept",list.get(0));
          return "update";
    }
//修改部门
 @PostMapping("/updateDept")
    public String updateDept(Dept dept,Model model){
     this.deptService.updateDept(dept);
     return this.index(model);
 }
 //条件查询
 @PostMapping("/query")
    public String query(Dept dept,Model model){
     //构造模糊查询条件
     if(dept.getDeptName() != null && !dept.getDeptName().equals("")){
         dept.setDeptName("%" + dept.getDeptName() + "%");
     }

     List<Dept> list = this.deptService.queryDept(dept);
     model.addAttribute("list",list);
     return "index";
 }

 //异步调用
    @RequestMapping("/ajaxQuery")
    @ResponseBody
    public List<Dept> ajaxQuery(){
        List<Dept> list = this.deptService.queryDept(new Dept());
        return list;
    }
 //异步添加部门
 @PostMapping("/ajaxSaveDept")
 @ResponseBody
 public String ajaxSaveDept(@RequestBody Dept dept){
     this.deptService.addDept(dept);
     return "sucess";
 }
    //异步删除

    @GetMapping("/ajaxDelDept/{deptNo}")
    @ResponseBody
    public String ajaxDelDept (@PathVariable String deptNo){
        this.deptService.delDept(Integer.valueOf(deptNo));
        return "sucess";
    }
    //异步调用查询部门信息
    @GetMapping("/ajaxQueryDept/{deptNo}")
    @ResponseBody
    public Dept ajaxQueryDept(@PathVariable String deptNo){
        Dept dept = new Dept();
        dept.setDeptNo(Integer.valueOf(deptNo));
        List<Dept> list = this.deptService.queryDept(dept);
        return list.get(0);
    }
    //异步修改
    @PostMapping("/ajaxUpdateDept")
    @ResponseBody
    public String ajaxUpdateDept(@RequestBody Dept dept){
        this.deptService.updateDept(dept);
        return "sucess";
    }
}
